package com.portal.sso.whitelist.config;

import com.portal.sso.core.server.AbstractTokenManage;
import com.portal.sso.whitelist.annotation.EnableWhitelistAdmin;
import com.portal.sso.whitelist.controller.AdminController;
import com.portal.sso.whitelist.server.WhitelistModelServer;
import com.portal.sso.whitelist.server.WhitelistServer;
import com.portal.sso.whitelist.server.UserDetaiServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

@Configuration
@EnableWhitelistAdmin
public class WhitelistAutoConfiguration {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Bean(name="whitelistModelServer")
   public WhitelistModelServer whitelistModelServer(@Qualifier("whitelistJdbcTemplate") JdbcTemplate jdbcTemplate){
        WhitelistModelServer bean = new WhitelistModelServer(jdbcTemplate,redisTemplate);
        bean.Init();
        return  bean;
    }
    //第二个ds2数据源配置
    @Bean(name = "whitelistDataSourceProperties")
    @ConfigurationProperties(prefix = "portal.datasource.whitelist")
    public DataSourceProperties ds2DataSourceProperties() {
        return new DataSourceProperties();
    }
    @Bean(name = "whitelistDataSource")
    public DataSource whitelistDataSource(@Qualifier("whitelistDataSourceProperties") DataSourceProperties dataSourceProperties) {
        return dataSourceProperties.initializeDataSourceBuilder().build();
    }
    @Bean(name = "whitelistJdbcTemplate")
    public JdbcTemplate whitelistJdbcTemplate(
            @Qualifier("whitelistDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
    @Bean
    public AbstractTokenManage whitelistServer(@Qualifier("whitelistModelServer") WhitelistModelServer whitelistModelServer){
        WhitelistServer bean = new WhitelistServer(whitelistModelServer);
        return  bean;
    }
}
